/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
import entities.Customer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import hibernateUtil.HibernateSessionFactory;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author A
 */
public class CustomerDao{
    private static final Log log = LogFactory.getLog(CustomerDao.class);

    public void save(Customer customer){
        Session session = null;
        Transaction transaction = null;
        log.debug("persisting CustomerDao instance");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            session.save(customer);
            log.debug("save successful");
	} catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
	}
        finally{
            transaction.commit();
            session.close();
        }
    }
    
    public Customer findById(int id){
        Session session = null;
        Transaction transaction = null;
        log.debug("getting Customer instance with id: " + id);
        try{
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            Customer instance = (Customer) session.get(Customer.class, id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
		log.debug("get successful, instance found");
            }
		return instance;
        } catch (RuntimeException re){
            	log.error("get failed", re);
		throw re;
        } finally {
            transaction.commit();
            session.close();
        }
    }
    
    public ArrayList<Customer> getAllCustomers(){
        Session session = null;
        Transaction transaction = null;
        ArrayList<Customer> customerList = null;
        log.debug("getting Customers HashSet");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            customerList = (ArrayList<Customer>) session.createCriteria(Customer.class).list();
            if(customerList.isEmpty()){
                log.debug("get successful, empty customerList");
            } else {
                log.debug("get successful, not empty customerList");
            }
        } catch (RuntimeException re) {
              log.error("get failed", re);
              throw re;
        } finally {
            transaction.commit();
            session.close();
        }
        return customerList;
    }

    public void update(Customer customer){
        Session session = null;
        Transaction transaction = null;
        log.debug("update Customer");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            session.update(customer);
            log.debug("update successful");
	} catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
	}
        finally{
            transaction.commit();
            session.close();
        }        
    }
    
    public void delete(Customer customer){
        Session session = null;
        Transaction transaction = null;
        log.debug("delete Customer");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            session.delete(customer);
            log.debug("delete successful");
	} catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
	}
        finally{
            transaction.commit();
            session.close();
        }        
    }    
    
}
